<?php
class ControllerEmployee extends ControllerMain implements iController
{
	protected $employee;
	protected $appointment;
	
	protected $mail;
	protected $employeeEmail;
	protected $mailSubject;
	protected $mailMessage;

	protected $message;
	protected $error;

	public function index()
	{
		$this->viewList();
	}
	
	public function viewList()
	{
		$this->employee = new Employee();
		$this->keys['employees'] = $this->employee->getAllFields('id,name');
		$this->keys['number'] = BOARDROOM_NUMBER;
		$this->template = View::create($this->templateDir.'viewList.html',$this->keys);
		View::assign('content',$this->template);
		unset($this->template);
		unset($this->keys);
		unset($this->employee);
		unset($this->employees);	
	}
	
	public function remove()
	{
		if(!empty($this->get['id']))
		{
			$this->employee = new Employee();
			if($this->employee->get($this->get['id']))
			{
				if($this->employee->delete())
				{
					$this->appointment = new Appointment();
					$this->appointment->deleteAppointmentByEmployeeId($this->get['id']);
					$this->message = View::message('Employee successfully removed.');
					View::assign('content',$this->message);
				}
				else
				{
					$this->error = View::error('Delete error.');
					View::assign('content',$this->error);
				}		
			}
			else
			{
				$this->error = View::error('Employee not exists.');
				View::assign('content',$this->error);
			}	
		}
		else
		{
			$this->error = View::error('You don\'t choose employee for delete.');
			View::assign('content',$this->error);
		}
		
		$this->viewList();
	}
	
	public function editForm()
	{
		if(!empty($this->get['id']))
		{
			$this->employee = new Employee();
			if($this->employee->get($this->get['id']))
			{
				$this->keys = $this->employee->getFields('name,email');
				$this->keys[0]['number'] = BOARDROOM_NUMBER;
				$this->keys[0]['id'] = $this->get['id'];
				$this->template = View::create($this->templateDir.'editForm.html',$this->keys[0]);
				View::assign('content',$this->template);
				
				unset($this->template);
				unset($this->keys);
				unset($this->employee);
			}
			else
			{
				$this->error = View::error('Employee not exists.');
				View::assign('content',$this->error);
			}		
		}
		else
		{
			$this->error = View::error('You don\'t choose employee for delete.');
			View::assign('content',$this->error);
		}
	}
	
	public function edit()
	{
		if(!empty($this->get['id']) &&
		   !empty($this->post['name']) &&
		   !empty($this->post['email']))
		{
			if(preg_match("/^([a-z0-9_\.-]+)@([a-z0-9_\.-]+)\.([a-z\.]{2,6})$/",
						  $this->post['email']))
			{
				$this->employee = new Employee;
				if($this->employee->get($this->get['id']))
				{
					$this->employee->name = $this->post['name'];
					$this->employee->email = $this->post['email'];
					if($this->employee->save())
					{
						$this->message = View::message('Information successfully edited.');
						View::assign('content',$this->message);
						unset($this->employee);
					}
					else
					{
						$this->error = View::error('Information don\'t save.');
						View::assign('content',$this->error);
						unset($this->employee);		
					}	
				}
				else
				{
					$this->error = View::error('Employee not exists.');
					View::assign('content',$this->error);
					unset($this->employee);
				}
			}
			else
			{
				$this->error = View::error('You enter incorrect email.');
				View::assign('content',$this->error);
			}
		}
		else
		{
			$this->error = View::error('You don\'t enter all information.');
			View::assign('content',$this->error);
		}
		if(!empty($this->error))
		{
			unset($this->error);
			$this->editForm();
		}
		else
		{
			unset($this->message);
			$this->viewList();
		}	
	}
	
	public function addForm()
	{
		$this->keys['number'] = BOARDROOM_NUMBER;
		$this->template = View::create($this->templateDir.'addForm.html',$this->keys);
		View::assign('content',$this->template);
		unset($this->keys);
		unset($this->template);
	}
	
	public function add()
	{
		if(!empty($this->post['name']) && !empty($this->post['email']))
		{
			if(preg_match("/^([a-z0-9_\.-]+)@([a-z0-9_\.-]+)\.([a-z\.]{2,6})$/",
						  $this->post['email']))
			{
				$this->employee = new Employee;
				$this->employee->name = $this->post['name'];
				if($this->employee->checkMail($this->post['email']))
				{
					$this->employee->email = $this->post['email'];

					if($this->employee->save())
					{
						$this->message = View::message('Information successfully added.');
						View::assign('content',$this->message);
						unset($this->employee);
					}
					else
					{
						$this->error = View::error('Information don\'t save.');
						View::assign('content',$this->error);
						unset($this->employee);
					}
				}
				else
				{

					$this->error = View::error('Email usually exists.');
					View::assign('content',$this->error);				
				}	
			}
			else
			{
				$this->error = View::error('You enter incorrect email.');
				View::assign('content',$this->error);
			}
		}
		else
		{
			$this->error = View::error('You don\'t enter all information..');
			View::assign('content',$this->error);
		}
		
		if(!empty($this->error))
		{
			unset($this->error);
			$this->addForm();
		}
		else
		{
			unset($this->message);
			$this->viewList();
		}
	}
	
	public function mailtoForm()
	{
		if(isset($this->get['id']) && !empty($this->get['id']))
		{
			$this->employee = new Employee();
			if($this->employee->get($this->get['id']))
			{
				$this->keys['employeeId'] = $this->get['id'];
				$this->keys['employeeName'] = $this->employee->getFields('name');
				$this->keys['employeeName'] = $this->keys['employeeName'][0]['name'];

				if(!isset($this->keys['message']))
				{
					$this->keys['message'] = '';
				}
				
				if(!isset($this->keys['subject']))
				{
					$this->keys['subject'] = '';
				}
				$this->template = View::create($this->templateDir.'mailtoForm.html',$this->keys);
				View::assign('content',$this->template);
			}
			else
			{
				$this->error = View::error('Employee not exists!');
				View::assign('content',$this->error);
			}
		}
		else
		{
			$this->error = View::error('You don\'t choose employee for mailto action.');
			View::assign('content',$this->error);
		}
	}
	
	public function send()
	{
		if(isset($this->post['id']) && !empty($this->post['id']) &&
		   isset($this->post['subject']) && !empty($this->post['subject']) &&
		   isset($this->post['message']) && !empty($this->post['message']))
		{
			$this->employee = new Employee();
			if($this->employee->get($this->post['id']))
			{
				$this->mail = new Mail();
				$this->employeeEmail = $this->employee->getFields('email');
				$this->employeeEmail = $this->employeeEmail[0]['email'];
				$this->mailSubject = $this->post['subject'];
				$this->mailMessage = $this->post['message'];
				if($this->mail->sendMail($this->employeeEmail,
										 $this->mailSubject,
										 $this->mailMessage))
				{
					$this->message = View::message('Message successfully send!');
					View::assign('content',$this->message);
				}
				else
				{
					
					$this->error = View::error('Error of mail service. Please check settings..');
					View::assign('content',$this->error);
				}
			}
			else
			{
				$this->error = View::error('Incorrect information about employee.');
				View::assign('content',$this->error);
			}
		}
		else
		{
			if(isset($this->post['subject']))
			{
				$this->keys['subject'] = $this->post['subject'];
			}

			if(isset($this->post['message']))
			{
				$this->keys['message'] = $this->post['message'];
			}
			$this->error = View::error('You don\'t enter all information.');
			View::assign('content',$this->error);
		}
		$this->mailtoForm();
	}
}